package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText heightEditText;
    private EditText weightEditText;
    private Button calculateButton;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);

        heightEditText = findViewById(R.id.h);
        weightEditText = findViewById(R.id.w);
        calculateButton = findViewById(R.id.button2);
        resultTextView = findViewById(R.id.r);

        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateBMI();
            }
        });
    }

    private void calculateBMI() {
        String heightStr = heightEditText.getText().toString();
        String weightStr = weightEditText.getText().toString();

        // 将身高体重转换为数值
        double height = Double.parseDouble(heightStr) / 100; // 转换为米
        double weight = Double.parseDouble(weightStr);

        // 计算BMI
        double bmi = weight / (height * height);

        // 根据BMI值给出建议
        String suggestion;
        if (bmi < 18.5) {
            suggestion = "体重过轻，建议增加营养，适量运动";
        } else if (bmi >= 18.5 && bmi <= 24.9) {
            suggestion = "体重正常，保持良好生活习惯";
        } else if (bmi >= 25 && bmi <= 29.9) {
            suggestion = "超重，建议控制饮食，增加运动量";
        } else {
            suggestion = "肥胖，建议积极减重，控制饮食，加强运动";
        }

        // 在TextView中显示结果和建议
        resultTextView.setText("BMI: " + String.format("%.2f", bmi) + "\n" + suggestion);
    }

}
